package com.changgou.controller;
import com.alibaba.fastjson.JSON;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.feign.CartFeign;
import com.changgou.feign.OrderFeign;
import com.changgou.feign.SkuFeign;
import com.changgou.feign.UserFeign;
import com.changgou.pojo.User;
import com.changgou.web.CasUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    private UserFeign userFeign;

    @Autowired
    private CartFeign cartFeign;

    /**
     * 发送短信验证码
     * @param mobile
     * @return
     */
    @GetMapping("/send_sms")
    public Result sendSms(String mobile){
        userFeign.sendSms(mobile);
        return new Result(  );
    }

    /**
     * 注册
     * @param user
     * @param smsCode
     * @return
     */
    @PostMapping("/add")
    public Result add( @RequestBody User user , String smsCode){
        userFeign.add( user,smsCode );
        return new Result(  );
    }


    /**
     * 获取当前登录名
     * @return
     */
    @GetMapping("/loginName")
    public Result loginName(){
        String loginName = CasUtil.loginName();
        return new Result( true, StatusCode.OK, "查询成功",loginName);
    }

    /**
     * 收藏夹列表
     * @param username
     * @return
     */
    @GetMapping("/collect")
    public Result getCollect(@RequestParam String username){
        Result collect = cartFeign.getCollect(username);
        return new Result(true,StatusCode.OK,"查询成功",collect.getData());
    }

    @Autowired
    private OrderFeign orderFeign;

    @Autowired
    private SkuFeign skuFeign;
    /**
     * 根据用户名查询订单
     * @param username
     * @return
     */
    @GetMapping("/findByUsername")
    public Result findByUsername(@RequestParam String username) throws ParseException {
        Result byUsername = orderFeign.findByUsername(username);
        List<Map> skuProduct = (List<Map>) byUsername.getData();
        for (Map map : skuProduct) {
            /**
             * 查询skuid以获取spec规格参数
             */
            String sku_id = (String) map.get("sku_id");
            Map sku = (Map) skuFeign.findById(sku_id).getData();
            Map spec = JSON.parseObject((String)sku.get("spec"),Map.class);
            map.put("spec", spec);
            /**
             * 订单创建时间格式转换
             */
            String create_time = (String)map.get("create_time");
            SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ", Locale.ENGLISH);
            Date date = sf1.parse(create_time);
            SimpleDateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            map.put("create_time",sf2.format(date));
        }
        return new Result(true, StatusCode.OK, "查询成功", byUsername);
    }


    /**
     * 查询未付款订单
     * @param username
     * @return
     */
    @GetMapping("/findUnpaid")
    public Result findUnpaid(@RequestParam String username) throws ParseException {
        Result unpaid = orderFeign.findUnpaid(username);
        List<Map> data = (List<Map>) unpaid.getData();
        for (Map datum : data) {
            String unpaidSkuid = (String)datum.get("sku_id");
            Map unpaidSku = (Map)skuFeign.findById(unpaidSkuid).getData();
            Map unpaidMap = JSON.parseObject((String) unpaidSku.get("spec"), Map.class);
            datum.put("spec",unpaidMap);
            /**
             * 订单创建时间格式转换
             */
            String create = (String)datum.get("create_time");
            SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ", Locale.ENGLISH);
            Date date = sf1.parse(create);
            SimpleDateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            datum.put("create_time",sf2.format(date));
        }
        return new Result(true, StatusCode.OK, "查询成功", unpaid);
    }

    /**
     * 提交订单
     * @return
     */
    @GetMapping("/finishOrder")
    public Result finishOrder (){
        return new Result(true,StatusCode.OK,"查询成功");
    }



    /**
     * 验证更改密码时输入的用户名数据库是否存在
     * @param username
     * @return
     */
    @GetMapping("/checkusername")
    public Result checkusername(String username){
        Result usernameResult = userFeign.findById(username);
        if (usernameResult.getData() == null && "".equals(usernameResult.getData())) {
            return new Result(false,StatusCode.ERROR,"");
        }
        return new Result(true,StatusCode.OK,"");
    }

    /**
     * 修改用户密码
     * @param newpw
     * @param oldpw
     * @param username
     * @return
     */
    @GetMapping("/checkpassword")
    public Result checkPassword(@RequestParam("newpw") String newpw,@RequestParam("oldpw") String oldpw,@RequestParam("username") String username){
        //判断两次密码是否一致
        if (newpw != oldpw && !newpw.equals(oldpw)) {
            return new Result(false, StatusCode.ERROR, "");
        }
        //密码正确
        User user = new User();
        user.setPassword(newpw);
        //Result updateResult = userFeign.update(user, username);
        return new Result(true, StatusCode.OK, "修改成功");
    }


    /**
     * 更改图片后,将图片地址更新到数据库
     * @param
     * @param
     * @return
     */
    @GetMapping("/updateimage")
    public Result updateimage(HttpServletRequest request){
        Map<String,String[]> parameterMap = request.getParameterMap();
        System.out.println(parameterMap+"+++++++++++++++");

        String[] imageUrls = parameterMap.get("imageUrl");
        String imageUrl = imageUrls[0];

        String[] usernames = parameterMap.get("username");
        String username = usernames[0];

        System.out.println("更新图片信息");

        //Result updateResult = userFeign.updateHeadPic(imageUrl, username);
        System.out.println("更新完成");
        return new Result(true, StatusCode.OK, "");
    }


}
